package myPack.GMAO.Bean;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import myPack.GMAO.Entity.Privilege;
import myPack.GMAO.Entity.User;
import myPack.GMAO.Interface.UserInterface;
@Stateless
public class UserBean implements UserInterface 
{
	@PersistenceContext
	EntityManager em;
	
	@Override
	public void ajouter(User u) {
		em.merge(u);
	}

	@Override
	public void supprimer(int id) {
		em.remove(em.find(User.class, id));
	}

	@Override
	public User rechercher(int id) {
		return em.find(User.class,id);
	}

	@Override
	public List<User> lister() {
		return  em.createQuery("from User").getResultList();
	}

	@Override
	public void modifier(int id, String log, String psw, Privilege p) {
		User tmp=em.find(User.class, id);
		tmp.setLog(log);
		tmp.setPass(psw);
		tmp.setPrivilege(p);
	}

	@Override
	public User connexion(String log, String psw) {
		List<User> users= em.createQuery("from User where log='"+log+"' and pass='"+psw+"'").getResultList();
		if(users.size()!=0)return users.get(0);
		return null;
	}

}
